﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
#endregion

namespace Rio.WCF.Services.Report
{
    public class ReportHandler : PdfPageEventHelper
    {
        #region Members
        //static ReportParameter l_oExportParameter = new ReportParameter();
        protected PdfTemplate l_oFooter;
        protected PdfTemplate l_oHeader;
        public int m_iSummaryPageNumber = 0;
        protected BaseFont l_oHelv;
        public int m_iCurrentPage = 0;
        public Dictionary<int, string> l_oDicSummary = new Dictionary<int, string>();
        public bool m_bWritePageNumber = true;
        public string m_sHeaderTitle = string.Empty;
        #endregion

        public void SetFolderName(PdfWriter p_oWriter, string p_sFolderName)
        {
            var l_oPdfContentByte = p_oWriter.DirectContent;
            var l_iCurrentPageNumber = l_oPdfContentByte.PdfDocument.PageNumber;
            l_oDicSummary.Add(l_iCurrentPageNumber, p_sFolderName);
        }

        public override void OnOpenDocument(PdfWriter p_oWriter, iTextSharp.text.Document p_oDocument)
        {
            l_oFooter = p_oWriter.DirectContent.CreateTemplate(100, 100);
            l_oFooter.BoundingBox = new iTextSharp.text.Rectangle(-20, -20, 100, 100);

            l_oHeader = p_oWriter.DirectContent.CreateTemplate(100, 100);
            l_oHeader.BoundingBox = new iTextSharp.text.Rectangle(-20, -20, 100, 100);

            l_oHelv = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        }

        public override void OnEndPage(PdfWriter p_oWriter, iTextSharp.text.Document p_oDocument)
        {
            var l_oTableHeader = new PdfPTable(1);
            l_oTableHeader.WidthPercentage = 100;
            l_oTableHeader.TotalWidth = p_oDocument.PageSize.Width - p_oDocument.LeftMargin - p_oDocument.RightMargin; //this centers [table]

            var l_oCell = new PdfPCell(new Phrase(m_sHeaderTitle, new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.GRAY)));
            l_oCell.Colspan = 2;
            l_oCell.Border = Rectangle.BOTTOM_BORDER;
            l_oCell.Padding = 10;
            l_oCell.BorderColor = BaseColor.GRAY;
            l_oCell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            l_oTableHeader.AddCell(l_oCell);
            l_oTableHeader.WriteSelectedRows(0, 1, p_oDocument.LeftMargin, p_oDocument.PageSize.Height, p_oWriter.DirectContent);

            var l_oTableFooter = new PdfPTable(1);
            l_oTableFooter.WidthPercentage = 100;
            l_oTableFooter.TotalWidth = p_oDocument.PageSize.Width - p_oDocument.LeftMargin - p_oDocument.RightMargin; //this centers [table]

            var l_sPageNumber = string.Empty;

            if (m_bWritePageNumber)
                l_sPageNumber = (p_oWriter.PageNumber + m_iSummaryPageNumber).ToString();

            var l_oCellFooter = new PdfPCell(new Phrase(l_sPageNumber, new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.GRAY)));
            l_oCellFooter.Colspan = 2;
            l_oCellFooter.Border = Rectangle.TOP_BORDER;
            l_oCellFooter.Padding = 10;
            l_oCellFooter.BorderColor = BaseColor.GRAY;
            l_oCellFooter.HorizontalAlignment = 0;
            l_oTableFooter.AddCell(l_oCellFooter);
            l_oTableFooter.WriteSelectedRows(0, 1, p_oDocument.LeftMargin, 30, p_oWriter.DirectContent);
        }

        public override void OnStartPage(PdfWriter p_oWriter, Document p_oDocument)
        {
            m_iCurrentPage++;
        }
    }
}